<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebRTC Client Test</title>
</head>

<body>
    <h1>WebRTC Client Test</h1>
    <button >start</button>
    <script>
        const signalingServer = new WebSocket('ws://localhost:8080');
        const peerConnection = new RTCPeerConnection();

        signalingServer.onmessage = async (message) => {
            const data = JSON.parse(message.data);

            if (data.type === 'offer') {
                await peerConnection.setRemoteDescription(data.payload);
                const answer = await peerConnection.createAnswer();
                await peerConnection.setLocalDescription(answer);
                signalingServer.send(JSON.stringify({ type: 'answer', payload: answer }));
            } else if (data.type === 'answer') {
                await peerConnection.setRemoteDescription(data.payload);
            } else if (data.type === 'candidate') {
                await peerConnection.addIceCandidate(data.payload);
            }
        };

        peerConnection.onicecandidate = (event) => {
            if (event.candidate) {
                signalingServer.send(JSON.stringify({ type: 'candidate', payload: event.candidate }));
            }
        };

        // 创建一个 SDP offer
        async function createOffer() {
            const offer = await peerConnection.createOffer();
            await peerConnection.setLocalDescription(offer);
            signalingServer.send(JSON.stringify({ type: 'offer', payload: offer }));
        }

        createOffer();
    </script>
</body>

</html>